package cqrtplm.aop;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ty.basic.response.ResponseResult;
import cqrtplm.mapper.StoragePlanningMapper;
import cqrtplm.service.CategoryPictureService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Aspect
@Component
@Slf4j
public class CreateAndStartContainerViewAOP {

    @Resource
    private StoragePlanningMapper storagePlanningMapper;

    @Resource
    private CategoryPictureService categoryPictureService;

    @Around(value = "execution(* com.hustcad.plm.pdm.flowbusiness.controller.TyplmContainerViewCommonController.createAndStartContainerView(..))")
    public Object createAndStartContainerView(ProceedingJoinPoint point) throws Throwable {
        try {
            HttpServletRequest request=(HttpServletRequest)point.getArgs()[1];
            String param = request.getParameter("param");
            JSONObject jsonObject = JSONObject.parseObject(param);
            if (jsonObject!=null){
                JSONArray flowObjectList = jsonObject.getJSONArray("flowObjectList");
                if (CollUtil.isNotEmpty(flowObjectList)){

                    for (int i = 0; i < flowObjectList.size(); i++) {
                        JSONObject js = flowObjectList.getJSONObject(i);
                        if (js.get("type")!=null && (js.get("type").equals("物料码")||js.get("type").equals("图样代号") || js.get("type").equals("标准件"))){
                            String refObjectOid = js.getString("refObjectOid");
                            String code = js.getString("refObjectNumber");
                            String result = categoryPictureService.selectIsPicture(code);
                            if (result.equals("是")){
                                Integer count = storagePlanningMapper.selectDerivedImageCount(refObjectOid);
                                if (count<1){
                                    return ResponseResult.fail("400","物料码:"+js.getString("refObjectNumber")+"未上传缩略图!");
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return point.proceed();
        }
        return point.proceed();
    }
}
